package com.ui.design.view.JRTTChannel.demochannel;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.ui.design.R;
import com.ui.design.view.JRTTChannel.helper.ItemDragHelperCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * 频道管理弹窗
 * tangxianfeng 2021.7.19
 */
public class ChannelDialog extends Dialog {


    private RecyclerView mRecy;
    private final Context mContext;
    private ChannelAdapter adapter;
    List<ChannelEntity> items = new ArrayList<>();
    List<ChannelEntity> otherItems = new ArrayList<>();
    private final BackToChannel backToChannel;

    public ChannelDialog(@NonNull Context context, BackToChannel backToChannel) {
        super(context);
        this.mContext = context;
        this.backToChannel = backToChannel;
    }

    /**
     * 初始化信息
     */
    private void initDataInfo() {
        for (int i=0;i<11;i++){
            items.add(new ChannelEntity("频道"+i));
            otherItems.add(new ChannelEntity("其他"+i));
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.jrtt_channel_activity_channel);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(true);
        //设置window背景，默认的背景会有Padding值，不能全屏。当然不一定要是透明，你可以设置其他背景，替换默认的背景即可。
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        //一定要在setContentView之后调用，否则无效
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setWindowAnimations(R.style.BottomInAndOutStyle);
        mRecy = (RecyclerView) findViewById(R.id.recy);

        initDataInfo();
        init();
        findViewById(R.id.channel_close).setOnClickListener(v -> {
            dismiss();
        });
    }



    @Override
    public void dismiss() {
        super.dismiss();
        adapter.reset();
        //savaData();
        backToChannel.back(adapter.getMyChannelList());
    }

    private void init() {

        GridLayoutManager manager = new GridLayoutManager(mContext, 3);
        mRecy.setLayoutManager(manager);

        ItemDragHelperCallback callback = new ItemDragHelperCallback();
        final ItemTouchHelper helper = new ItemTouchHelper(callback);
        helper.attachToRecyclerView(mRecy);

        adapter = new ChannelAdapter(mContext, helper, items, otherItems);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int viewType = adapter.getItemViewType(position);
                return viewType == ChannelAdapter.TYPE_MY || viewType == ChannelAdapter.TYPE_OTHER ? 1 : 3;
            }
        });
        mRecy.setAdapter(adapter);

        adapter.setOnMyChannelItemClickListener((v, position) -> Toast.makeText(mContext, items.get(position).getName(), Toast.LENGTH_SHORT).show());
    }


    @Override
    public void show() {
        if (isShowing()) {
            return;
        }
        super.show();
    }

    public interface BackToChannel {
        void back(List<ChannelEntity> entities);
    }
}